package com.bang.realm;

import com.bang.pojo.User;
import com.bang.service.IPermissionService;
import com.bang.service.IRoleService;
import com.bang.service.IUserService;
import com.bang.utils.ActiveUser;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.AuthenticationInfo;
import org.apache.shiro.authc.AuthenticationToken;
import org.apache.shiro.authc.SimpleAuthenticationInfo;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.authz.SimpleAuthorizationInfo;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.subject.PrincipalCollection;
import org.apache.shiro.util.ByteSource;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;

/**
 * @Author: bangbang
 * @Date: 2020/5/14 16:15
 */
public class UserRealm extends AuthorizingRealm {

    @Autowired
    private IUserService userService;
    @Autowired
    private IRoleService roleService;
    @Autowired
    private IPermissionService pressionService;

    @Override
    public String getName(){
        return this.getClass().getSimpleName();
    }

    /**
     * 认证
     * @param authenticationToken
     * @return
     * @throws AuthenticationException
     */
    @Override
    protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken authenticationToken) throws AuthenticationException {
        //22222222222222222
        String username=authenticationToken.getPrincipal().toString();
        //根据用户名查询用户,逐步往下
        System.out.println(username);
        User user = this.userService.queryUserByUserName(username);
        System.out.println("==================================================");
        if (user!=null){
            //查询角色
            List<String> roles = this.roleService.queryRolesByUserId(user.getUserid());
            //查询权限
            List<String> permissions = this.pressionService.queryPermissionByUserId(user.getUserid());
            //构造ActiverUser
            ActiveUser activeUser = new ActiveUser(user, roles, permissions);
            //创建盐
            ByteSource credentialsSalt = ByteSource.Util.bytes(user.getUsername()+user.getAddress());
            SimpleAuthenticationInfo info = new SimpleAuthenticationInfo(activeUser, user.getUserpwd(), credentialsSalt, this.getName());
            System.out.println(user.getUserpwd());//盐值加密后的值
            return info;
        }else {
            return null;
        }
    }

    /**
     * 授权
     * @param principalCollection
     * @return
     */
    @Override
    protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principalCollection) {
        ActiveUser activerUser =(ActiveUser) principalCollection.getPrimaryPrincipal();
        //System.out.println(activerUser);

        SimpleAuthorizationInfo info = new SimpleAuthorizationInfo();

        List<String> roles = activerUser.getRoles();
        List<String> permissions = activerUser.getPermissions();
        if (roles!=null&&roles.size()>0){
            info.addRoles(roles);
        }
        if (permissions!=null&&permissions.size()>0){
            info.addStringPermissions(permissions);
        }
        return info;
    }

}
